//
//  LWStatusRetweetView.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/23.
//  Copyright © 2016年 Linwang. All rights reserved.
//

import UIKit
// 转发微博视图
class LWStatusRetweetView: UIView {


    
    
    var statusViewModel : LWStatusViewModel?{
        didSet{
            contentLabel.text = statusViewModel?.retweetContent
            
            //  绑定转发微博配图
            pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls

            
            //  卸载上次约束
            retweetViewBottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  表示有配图, 显示配图,更新约束,绑定数据
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(LWHomeTabCellMargin).constraint
                })
                //  绑定数据
                pictureView.picUrls = picUrls
                
                
            } else {
                //  表示没有配图 , 隐藏配图, 更新约束
                pictureView.isHidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(LWHomeTabCellMargin).constraint
                })
                
                
            }

            
        }
    }
    
    //  记录转发微博底部约束
    var retweetViewBottomConstraint: Constraint?

    
    private lazy var contentLabel : UILabel = {
       
        //多行显示
        let label  = UILabel(textColor: UIColor.darkGray, fontSize: 13)
        label.numberOfLines  = 0
        
        label.text = "转发微博啦啦啦啦啦"
        return label
    }()
    
    //  配图
    private lazy var pictureView: LWStatusPictureView = {
        let view = LWStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //添加控件设置约束
    private func setupUI(){
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)

        //设置约束
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(LWHomeTabCellMargin)
            make.leading.equalTo(self).offset(LWHomeTabCellMargin)
            make.width.equalTo(ScreenWidth - 2 * LWHomeTabCellMargin)
        }
        
        
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(LWHomeTabCellMargin)
            
        }

        
        
        //关键约束- > 当前内容的底部约束 = 转发微博内容的底部约束+ 间距
        
        self.snp_makeConstraints { (make ) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(LWHomeTabCellMargin).constraint


        }
    }
    
}
